package com.net.oil.user.adapter;

import android.content.Context;
import android.content.Intent;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.http.AppUrl;
import com.net.oil.user.MyReceiveGoodsDetailActivity;
import com.net.oil.user.bean.CouponRecordRefBusiness;
import com.net.oil.user.bean.OilApplyBusinessProductRecordVO;
import com.net.oil.util.DateFormat;
import com.net.oil.util.glide.GlideUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者：lch
 * 时间：2018/10/11 0011:下午 21:28
 * 邮箱：245850413@qq.com
 * 说明：
 */

public class MyReceiveGoodsRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;  //底部FootView
    private static final int TYPE_ITEM = 1;  //普通item

    private int load_more_status = 10;      //上拉加载更多状态-默认为10

    private Context context;
    private LayoutInflater inflater;
    private List<OilApplyBusinessProductRecordVO> data;

    private int used;   //used:到店领取状态(1:已到店领取 0：未领取 -1：已过期)

    public MyReceiveGoodsRecyclerViewAdapter(Context context, int used) {
        this.context = context;
        this.used = used;
        inflater = LayoutInflater.from(context);
        data = new ArrayList<>();
    }

    public void updateData(List<OilApplyBusinessProductRecordVO> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void loadMore(List<OilApplyBusinessProductRecordVO> data) {
        this.data.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TYPE_ITEM:
                view = inflater.inflate(R.layout.myreceive_goods_list_item, parent, false);
                return new ViewHolder(view);
            case TYPE_FOOTER:
                view = inflater.inflate(R.layout.recycler_view_foot_item, parent, false);
                return new FootViewHolder(view);
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            OilApplyBusinessProductRecordVO listBean = data.get(position);
            if (listBean != null) {
                bindItemData(viewHolder, listBean);
            }
        } else if (holder instanceof FootViewHolder) {
            FootViewHolder footViewHolder = (FootViewHolder) holder;
            footDataBind(footViewHolder);
        }
    }

    private void bindItemData(final ViewHolder viewHolder, final OilApplyBusinessProductRecordVO listBean) {
        if (!TextUtils.isEmpty(listBean.getProductName())) {
            viewHolder.goodsTitleTv.setText(listBean.getProductName());
        }

        String endTime = DateFormat.formatDateTime(listBean.getExpireTime(), DateFormat.FormatFlag.YDM);
        viewHolder.endTimeTv.setText(String.format(context.getString(R.string.receive_end_time), endTime));


        GlideUtil.getInstance().loadImage(context, viewHolder.goodsImg, AppUrl.getImageUrl(listBean.getProductImage()), R.drawable.product_err);

        /* 可使用的商家 */
        List<CouponRecordRefBusiness> list = listBean.getCouponRecordRefBusinessList();
        if (list != null){
            String names = null;
            for (CouponRecordRefBusiness business : list){
                if (!TextUtils.isEmpty(business.getBusinessName())){
                    if (TextUtils.isEmpty(names)){
                        names = business.getBusinessName();
                    }else {
                        names = names + business.getBusinessName() + "，";
                    }
                }
            }
            viewHolder.contentTv.setText(names);
        }else {
            viewHolder.contentTv.setText("全部商家可用");
        }

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, MyReceiveGoodsDetailActivity.class);
                intent.putExtra(Constant.INTENT_MY_RECEIVE_GOODS_ID_KEY, listBean.getId());
                context.startActivity(intent);
            }
        });

        viewHolder.unfoldImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (viewHolder.contentTv.getMaxLines() == 1){
                    viewHolder.contentTv.setMaxLines(Integer.MAX_VALUE);
                    viewHolder.unfoldImg.setRotation(-90);
                }else {
                    viewHolder.contentTv.setMaxLines(1);
                    viewHolder.unfoldImg.setRotation(90);
                }

            }
        });

    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size() + 1;
    }

    /**
     * 更改滑动的状态
     *
     * @param status 上拉加载更多 PULL_LOAD_MORE = 10; 正在加载中 LOADING_MORE = 11;
     *               没有更多 LOADING_NONE = 12; 上拉加载更多状态-默认为10
     */
    public void changeMoreStatus(int status) {
        load_more_status = status;
        notifyDataSetChanged();
    }

    /**
     * 绑定加载更多项的数据
     *
     * @param footViewHolder holder
     */
    private void footDataBind(FootViewHolder footViewHolder) {
        switch (load_more_status) {
            case Constant.PULL_LOAD_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("上拉加载更多...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_MORE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("正在加载更多数据...");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.VISIBLE);
                break;
            case Constant.LOADING_NONE:
                footViewHolder.itemView.setVisibility(View.VISIBLE);
                footViewHolder.more.setText("没有更多了");
                footViewHolder.more.setEnabled(false);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
            case Constant.LOADING_FAIL:
                footViewHolder.more.setText("加载失败");
                footViewHolder.more.setEnabled(true);
                footViewHolder.progressBar.setVisibility(View.GONE);
                break;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.goods_img)
        ImageView goodsImg;
        @BindView(R.id.goods_title_tv)
        TextView goodsTitleTv;
        @BindView(R.id.content)
        TextView contentTv;
        @BindView(R.id.end_time_tv)
        TextView endTimeTv;
        @BindView(R.id.unfold_img)
        ImageView unfoldImg;
        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.progress_bar)
        ProgressBar progressBar;
        @BindView(R.id.more)
        TextView more;

        FootViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
